package cc.telecomdigital.tdstock.Framework;


import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Tools.ActivityController;
import cc.telecomdigital.tdstock.Tools.ActivityGroupController;

import android.app.Activity;
import android.app.ActivityGroup;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


//This would only be treated as container
public abstract class ActivityGroupHost_Framework extends ActivityGroup
{
	final static protected boolean	debugMode	= false;

//	protected Context				actContext;

	/** Tell the host of this group, usually it is the Application
	  *  The Class use control list and should have a host list to implement
	  *  It could be overridden
	  **/
	protected ActivityController GetActivityHostControl()
	{
		return appHost.actListController;
	}

	/** The first activity to be shown when using this Extends Class **/
	protected abstract Class<? extends ActivityGroup_Framework>  GetPrimaryActivity();

	/** Create the internal controller for Activity Switching, sharable by other Group_Framework **/
	protected abstract ActivityGroupController CreateActivityGroupHostController();

	/** Group control to be use for internal Activity Switching, Served as a pointer **/
	private ActivityGroupController	_groupController;

	protected ActivityController				_actControl	= null;
	protected final Application_Framework		appHost;

	private String							className		= null;

	private String GetIdentity()
	{
		if (className == null)
			className = getClass().getName();
		return className;
	}

	protected ActivityGroupHost_Framework()
	{
		super();
		appHost = Application_Framework.GetInstance();	
	}

	@Override
	protected void onCreate(Bundle iSaveInstance)
	{
		super.onCreate(iSaveInstance);
		_actControl = GetActivityHostControl();
		// Create Host Controller and set current as the top
		_groupController = CreateActivityGroupHostController();
		if (debugMode)
			TDStockLog.i(GetIdentity(), "onCreated called");
//		actContext = this;
	}

	@Override
	protected void onStart()
	{
		super.onStart();  //modified by haibo in 2012-03-29 to avoid  Exception
//		UpdateDisplayDimension();

		if (debugMode)
			TDStockLog.i(GetIdentity(), "onStart called");

		boolean animiEnable;
		Class<? extends Activity> classObj;
		
		animiEnable = _groupController.IsTransitWithAnimation();
		_groupController.TransitWithAnimation(false);		
		//If it has been previous used, we should bring it back to the previous
		if (_groupController.GetListCount() != 0)
		{
			if (debugMode)
				TDStockLog.i(GetIdentity(), "Going with the previous used window");

			classObj = _groupController.GetCurrentActivityClass();
			if (classObj != null)
			 _groupController.ActivityEnforceSwitch(classObj);
		}
		else
		{
			if (debugMode)
				TDStockLog.i(GetIdentity(), "Going with the prime setting window");

			classObj = GetPrimaryActivity();
			if (classObj != null)
			{
				_groupController.ActivityEnforceSwitch(classObj);				
				_groupController.ActivityResetToTop(classObj);		
			}
		}
		_groupController.TransitWithAnimation(animiEnable);
		//super.onStart();
	}

//	static protected int	fScrWidthPixel		= 0;
//	static protected int	fScrHeightPixel	= 0;

	@Override
	protected void onDestroy()
	{
		//This is to un-subscribe all items
		if (debugMode)
			TDStockLog.i(GetIdentity(), "ActivityGroupHost onDestroy");

		getLocalActivityManager().dispatchDestroy(true);
		//Clear the internal activity list
		getLocalActivityManager().removeAllActivities();
		super.onDestroy();
	}

	// Basically get system configuration
//	protected void UpdateDisplayDimension()
//	{
//		fScrWidthPixel = getWindowManager().getDefaultDisplay().getWidth();
//		fScrHeightPixel = getWindowManager().getDefaultDisplay().getHeight();
//	}

	// Return the stored values;
	// The system will be constantly report to us
	protected boolean IsNetworkAvailable()
	{
		return appHost.IsNetworkAvailable();
	}

	protected boolean IsDisplayOn()
	{
		return appHost.IsDisplayOn();
	}

	// Depend on Manifest
//	@Override
//	public void onConfigurationChanged(Configuration iNewChange)
//	{
//		super.onConfigurationChanged(iNewChange);
//		UpdateDisplayDimension();
//	}

//	protected void SetPrimaryContent(Class<? extends Activity> iActivityClass)
//	{
//		SetPrimaryContent(iActivityClass, new Intent());
//	}
//
//	protected void SetPrimaryContent(Class<? extends Activity> iActivityClass, Intent iIntentObj)
//	{
//		boolean animiEnable;
//
//		if (_groupController != null)
//		{
//			animiEnable = _groupController.IsTransitWithAnimation();
//			_groupController.TransitWithAnimation(false);		
//			_groupController.ActivityForwardSwitch(this, iActivityClass, iIntentObj);				
//			_groupController.ActivityResetToTop(iActivityClass);		
//			_groupController.TransitWithAnimation(animiEnable);
//		}
//	}

	@Override
	public void onBackPressed()
	{
		if (debugMode)
			TDStockLog.i(GetIdentity(), "ActivityGroup onBackPressed");

//		// If the Controller list is all use up, use the big list
//		if (_groupController != null)
//		{
//			if (_groupController.GetListCount() <= 1)				
//			{
//				//must interfere with the back pressed				
//				if (_actControl != null)
//					_actControl.ActivityBackwardSwitch(this);
//				return;
//			}
//			//Must interfere with the back pressed
//			_groupController.ActivityBackwardSwitch(this);
//		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		return getLocalActivityManager().getCurrentActivity().onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		return getLocalActivityManager().getCurrentActivity().onOptionsItemSelected(item);
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item)
	{
	        return getLocalActivityManager().getCurrentActivity().onMenuItemSelected(featureId, item);
	}
}
